/**
 * Copyright (C) 2010 David Simonek <dafesimonek@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.dafe.partyband.gui.imports;

import com.db4o.ObjectContainer;
import java.io.IOException;
import java.util.Collections;
import java.util.Map.Entry;
import java.util.Set;
import org.dafe.partyband.db.api.DbManager;
import org.dafe.partyband.gui.imports.DetailsPanel.ImportResult;
import org.dafe.partyband.importer.api.IImportRecipe;
import org.dafe.partyband.importer.api.IImporterServices;
import org.dafe.partyband.importer.api.ISongImport;
import org.dafe.partyband.importer.api.ISongImporter;
import org.netbeans.api.progress.ProgressHandle;
import org.openide.WizardDescriptor;
import org.openide.WizardDescriptor.Panel;
import org.openide.util.Lookup;

/**
 *
 * @author dafe
 */
public class ImportWizardIterator extends WizardDescriptor.ArrayIterator<WizardDescriptor> implements WizardDescriptor.ProgressInstantiatingIterator<WizardDescriptor> {
    private WizardDescriptor wd;

    public ImportWizardIterator(Panel<WizardDescriptor>[] array) {
        super(array);
    }

    public Set instantiate(ProgressHandle ph) throws IOException {
        ImportResult ir = (ImportResult) wd.getProperty(DetailsWizardPanel.RESULT);

        ph.start(2);

        // remove not enabled imports
        IImportRecipe recipe = ir.recipe;
        IImportRecipe newRecipe = recipe.createRecipe();
        Set<Entry<ISongImport, Boolean>> entries = ir.importEnabled.entrySet();
        for (Entry<ISongImport, Boolean> entry : entries) {
            if (entry.getValue()) {
                newRecipe.addSongImport(entry.getKey());
            }
        }

        ph.progress(1);

        // prepare importer
        IImporterServices services = Lookup.getDefault().lookup(IImporterServices.class);
        ISongImporter importer = services.getSongImporter();
        ObjectContainer db = DbManager.getObjectContainer();
        importer.setDbManager(DbManager.getBandManager());

        // do import and commit changes
        importer.doImport(newRecipe);
        db.commit();

        ph.finish();

        return Collections.EMPTY_SET;
    }

    public void attachDescriptor (WizardDescriptor wd) {
        this.wd = wd;
    }

    public Set instantiate() throws IOException {
        throw new IllegalStateException("Should never be called");
    }

    public void initialize(WizardDescriptor wd) {
    }

    public void uninitialize(WizardDescriptor wd) {
    }




}
